package com.gvitz.transaction.activities;


import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.Tab;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.gvitz.transaction.R;
import com.gvitz.transaction.utils.ViewPagerAdapter;

public class MainActivity extends GVActivity {
	 
    private static final int MENU_ADD = 1;
    private static final int ABOUT = 3;

    ActionBar mActionBar;
    ViewPager mPager;
    Tab tab;
	String TabFragmentP;
	String[] tabNames = new String[4];
	
 
    public String getTabFragmentP() {
		return TabFragmentP;
	}


	public void setTabFragmentP(String tabFragmentP) {
		TabFragmentP = tabFragmentP;
	}


	@Override
    protected void onCreate(Bundle savedInstanceState) {
	       
		   super.onCreate(savedInstanceState);     
	        setContentView(R.layout.activity_main);
 
        // Activate Navigation Mode Tabs
        mActionBar = getSupportActionBar();
        mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
 
        // Locate ViewPager in activity_main.xml
        mPager = (ViewPager) findViewById(R.id.pager);
 
        // Activate Fragment Manager
        FragmentManager fm = getSupportFragmentManager();
 
        // Capture ViewPager page swipes
        ViewPager.SimpleOnPageChangeListener ViewPagerListener = new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                mActionBar.setSelectedNavigationItem(position);
            }
        };
 
        
        mPager.setOnPageChangeListener(ViewPagerListener);
        
        ActionBar.TabListener tabListener = new ActionBar.TabListener() {
        	 
            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) {
                // Pass the position on tab click to ViewPager
                mPager.setCurrentItem(tab.getPosition());
            }
 
            @Override
            public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                // TODO Auto-generated method stub
            }
 
            @Override
            public void onTabReselected(Tab tab, FragmentTransaction ft) {
                // TODO Auto-generated method stub
            	
            }
        };
        

        	
        	ViewPagerAdapter viewpageradapter = new ViewPagerAdapter(fm);
        	mPager.setOffscreenPageLimit(4); 
        	mPager.setAdapter(viewpageradapter);      	
                    	       	
        	tab = mActionBar.newTab().setText(R.string.fa_adjust).setTabListener(tabListener);
            mActionBar.addTab(tab); 
            tab = mActionBar.newTab().setText(R.string.tab_pagar).setTabListener(tabListener);
            mActionBar.addTab(tab);
            tab = mActionBar.newTab().setText(R.string.tab_tarjetas).setTabListener(tabListener);
            mActionBar.addTab(tab);
            tab = mActionBar.newTab().setText(R.string.tab_transacciones).setTabListener(tabListener);
            mActionBar.addTab(tab);

        
        Typeface font = Typeface.createFromAsset( getAssets(), "fonts/fontawesome-webfont.ttf" );
       
        tabNames[0]= getString(R.string.fa_globe);
        tabNames[1]= getString(R.string.fa_money);
        tabNames[2]= getString(R.string.fa_credit_card);
        tabNames[3]= getString(R.string.fa_list_alt);


        for(int i = 0; i<mActionBar.getTabCount(); i++){

            LayoutInflater inflater = LayoutInflater.from(this);
            View customView = inflater.inflate(R.layout.tab_title, null);

            TextView titleTV = (TextView) customView.findViewById(R.id.action_custom_title);
            titleTV.setText(tabNames[i]);
            titleTV.setTypeface(font);
            mActionBar.getTabAt(i).setCustomView(customView);
        }
        
 
    }
    
    
    /** Se crear el menu con las distintas opciones .*/	  
    public boolean onCreateOptionsMenu(Menu menu) {
			
			menu.add(Menu.CATEGORY_ALTERNATIVE, MENU_ADD, Menu.NONE, "Agregar Tarjeta")
					.setIcon(R.drawable.icon_mas)
					.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
			
			menu.add(Menu.CATEGORY_ALTERNATIVE,ABOUT, Menu.NONE, "About")
			.setIcon(android.R.drawable.ic_menu_info_details)
			.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);

			return true;
		}
    
    
    /** Funcion para ver que se realiza con cada accion del menu */
   	@Override
   	public boolean onOptionsItemSelected(MenuItem item) {

   		super.onOptionsItemSelected(item);		
   			
   		if (item.getItemId() == MENU_ADD) {
			Intent i = new Intent(this, RegisterCardActivity.class);
			overridePendingTransition (R.anim.open_next, R.anim.close_main);
			startActivity(i);
			finish();
		}
   		
		if (item.getItemId() == ABOUT) {
			Intent i = new Intent(this, AboutActivity.class);
			startActivity(i);
		}
   		
   			return true;
   		}
      
    
}       
   	